<template>
  <div class="app-container">
    <el-col :span="1.5">
      <el-button
        type="info"
        plain
        icon="el-icon-sort"
        size="mini"
        @click="toggleExpandAll"
      >展开/折叠</el-button>
    </el-col>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      style="width: 100%"
      :max-height="maxHeight"
    >
      <el-table-column prop="deptName" label="部门名称" width="220" fixed></el-table-column>
      <el-table-column prop="deptId" label="部门编号" width="100" fixed></el-table-column>
      <!-- 地域数据可左右滑动 -->
      <el-table-column
        v-for="region in regions"
        :key="region.value"
        :prop="'region' + region.value"
        :label="region.label"
        width="120"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getcountbygenderApi, getEducationalLeveList, getRegionList } from "@/api/Empment/DataStatistics";

export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      regions: [
        { label: '湖北黄石', value: 0 },
        { label: '湖北（黄石以外）', value: 1 },
        { label: '北京市', value: 2 },
        { label: '天津市', value: 3 },
        { label: '河北省', value: 4 },
        { label: '山西省', value: 5 },
        { label: '内蒙古自治区', value: 6 },
        { label: '辽宁省', value: 7 },
        { label: '吉林省', value: 8 },
        { label: '黑龙江省', value: 9 },
        { label: '上海市', value: 10 },
        { label: '江苏省', value: 11 },
        { label: '浙江省', value: 12 },
        { label: '安徽省', value: 13 },
        { label: '福建省', value: 14 },
        { label: '江西省', value: 15 },
        { label: '山东省', value: 16 },
        { label: '河南省', value: 17 },
        { label: '湖南省', value: 18 },
        { label: '广东省', value: 19 },
        { label: '广西壮族自治区', value: 20 },
        { label: '海南省', value: 21 },
        { label: '重庆市', value: 22 },
        { label: '四川省', value: 23 },
        { label: '贵州省', value: 24 },
        { label: '云南省', value: 25 },
        { label: '西藏自治区', value: 26 },
        { label: '陕西省', value: 27 },
        { label: '甘肃省', value: 28 },
        { label: '青海省', value: 29 },
        { label: '宁夏回族自治区', value: 30 },
        { label: '新疆维吾尔自治区', value: 31 },
        { label: '香港特别行政区', value: 32 },
        { label: '澳门特别行政区', value: 33 },
        { label: '台湾省', value: 34 }
      ],
      // getDataStatisticsApi的返回数据
      deptData: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 视口高度
      viewportHeight: window.innerHeight
    };
  },
  computed: {
    maxHeight() {
      return this.viewportHeight * 0.8; // 70% of the viewport height
    }
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    /** 查询部门列表 */
    async getList() {
      this.loading = true;
      getRegionList().then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 处理窗口大小变化 */
    handleResize() {
      this.viewportHeight = window.innerHeight;
    }
  }
};
</script>

